//
//  AYSSpecialViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSSpecialViewController.h"
#import "AYSRequestManager.h"
#import "AYSSpecialMainModel.h"
#import "AYSSpecialMainViewCell.h"
#import "AYSSpecialDtailViewController.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"
#import "AppDelegate.h"
#import "AYSSideViewController.h"
@interface AYSSpecialViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataSourceArray;
@property (nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic, assign)NSInteger pageindex;
@end

@implementation AYSSpecialViewController

// 懒加载
- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray)
    {
        self.dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"专题";
    UIColor *color = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"抽屉菜单"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(clickLeftButton)];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.032 alpha:1.000];
    
    [SVProgressHUD show];
    NSString *url = @"http://doukantv.com/api/recommend/?type=movie&ver=2.3.4&sys_ver=9.1&cli=iphone&pageindex=1&pagesize=20";
    [self requestDataWithUrl:url];
    [SVProgressHUD dismiss];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 20, (SCREEN_HEIGHT - 100) / 2 - 25);
    
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 10;
    //layout.sectionInset = UIEdgeInsetsMake(5, 8, 5, 8);
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithWhite:0.021 alpha:0.900];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    // 注册cell
    [_collectionView registerClass:[AYSSpecialMainViewCell class] forCellWithReuseIdentifier:@"special"];
    [self refreshData];
}

- (void)requestDataWithUrl:(NSString *)url
{
    [AYSRequestManager requestWithType:GET url:url parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"result"];
        for ( NSDictionary *dict in arr)
        {
            AYSSpecialMainModel *model = [[AYSSpecialMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:model];
        }
        [_collectionView reloadData];
    } error:^(NSError *error) {
        NSLog(@"专题主页面数据请求失败");
    }];
}

#pragma wark ====================刷新数据
- (void)refreshData
{
    _pageindex = 1;
    __weak UICollectionView *collectionView = self.collectionView;
    // 开始刷新
    [collectionView.header beginRefreshing];
    // 下拉刷新
    collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSString *url = @"http://doukantv.com/api/recommend/?type=movie&ver=2.3.4&sys_ver=9.1&cli=iphone&pageindex=1&pagesize=20";
        [self requestDataWithUrl:url];
        [collectionView.header endRefreshing];
        
    }];
    
    // 上拉刷新
    collectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageindex ++;
        NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/recommend/?type=movie&ver=2.3.4&sys_ver=9.1&cli=iphone&pageindex=%ld&pagesize=20", _pageindex];
        [self requestDataWithUrl:urlStr];
        [collectionView reloadData];
        // 结束刷新
        [collectionView.footer endRefreshing];
        
    }];
    // 默认先隐藏footer
    collectionView.footer.hidden = YES;
    
    
}

- (void)clickLeftButton
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    AYSSideViewController *sideViewController = [delegate sideViewController];
    [sideViewController showLeftViewController:true];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AYSSpecialMainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"special" forIndexPath:indexPath];
    cell.specialModel = _dataSourceArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AYSSpecialDtailViewController *detialVC = [[AYSSpecialDtailViewController alloc] init];
    detialVC.movieID = [_dataSourceArray[indexPath.row] programID];
    detialVC.mainModel = _dataSourceArray[indexPath.row];
    [self.navigationController pushViewController:detialVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
